

#Config.MERGE_FILE=False
#COMPRESSION=True

import os
from as3 import ASFile
from singleton import singleton
from core.config import Config
from core.js import JSFile
import shutil
import time



@singleton
class Project:
    def __init__(self,src,output,name="Main"):
        #print("__init__")
        self.name=name
        self.src=src
        if os.path.isdir(self.src):
            self.dir=self.src+"/"
        else:
            self.dir=os.path.dirname(src)+"/"
        self.output=output+"/"
        self.asFiles={}
        
        
        
    def build(self):
        ms=time.time()
        if Config.CLEAN:
            if os.path.exists(self.output+Config.FOLDER):
                shutil.rmtree(self.output+Config.FOLDER)
            os.makedirs(self.output+Config.FOLDER)
        if os.path.isdir(self.src):
            self.readFolder(self.src)
        else:
            self.readFile(self.src)
        for path in self.asFiles:
            self.asFiles[path].rescan()
        self.writeJS()
        print ""
        print "BUILD COMPLETE"
        print ">Time:"+str(time.time()-ms)+"s"
        
        
    def readFolder(self,folder):
        pass
    
    def readFile(self,f):
        asFile=ASFile(f)
        asFile.mainScript=True
        #self.writeFile(asFile)
        self.asFiles[f]=asFile
        asFile.read()
        #for f in asFile.getDependency():
        #    if not self.asFiles.has_key(f):
        #        self.readFile(f)
    
    
    def writeJS(self):
        w=""
        footer=""

        if Config.COMPRESSION>2 and Config.MERGE_FILE:
            
            for ind in Config.REPLACE_COMPRESSION_3:
                w+=Config.REPLACE_COMPRESSION_3[ind]+"="+ind+";"
            #w+="w$=window;d$=document;"
            
        for asPath in self.asFiles:
            
            jsFile=self.convertAS(self.asFiles[asPath])
            if not jsFile:
                continue
            w+=jsFile.getContent()
            footer+=self.jsFile.getFooter()
            if not Config.MERGE_FILE:
                path=self.output+jsFile.getOuputPath()
                open(path,"w+").write(self.jsFile.getContent())
                w=""
                
        if Config.MERGE_FILE:
            w+=footer
            while w.find(";;")+1:
                w=w.replace(";;",";")
            open(self.output+Config.FOLDER+self.name+".js","w+").write(w)
            
            
        footer=""
                
    def convertAS(self,asFile):
        
        self.jsFile=JSFile(asFile)
        path=self.output+self.jsFile.getOuputPath()
        if not Config.MERGE_FILE and not Config.CLEAN and os.path.exists(path) and os.path.getmtime(path)>asFile.modifedDate:
            return None
        self.jsFile.write()
        
        folder=os.path.dirname(path)
        
        if not os.path.exists(folder):
            os.makedirs(folder,7777)
        return self.jsFile
     
    def createASFile(self,path,caller=""):
        asFile=None
        if path.find(self.dir)!=0:
            path=self.dir+path
        asFile=ASFile(path,caller)
        
        try:
            asFile.modifedDate=os.path.getmtime(path)
        except:
            pass
        
        self.asFiles[path]=asFile
        asFile.read()
        return asFile
    
    def removeAsFile(self,path):
        del self.asFiles[path]
   
    def addASFile(self,path,asFile):
        self.asFiles[path]=asFile
        
    def getASFile(self,path):
        
        if path.find(self.dir)!=0:
            path=self.dir+path
        #print (path,self.asFiles)
        if not self.asFiles.has_key(path):
            return None
        
        return self.asFiles[path]
        
    def getSrcPath(self):
        return self.dir